﻿using System;
using System.Collections;
using System.Web;
using viBlog.Controllers;
using visinia.SmartEngine;

namespace viBlog.HttpModules
{
    /// <summary>
    /// The blog url rewriter
    /// </summary>
    public class UrlRewriter : BaseUrlRewriter
    {

        public override string RewriteUrl(HttpApplication httpApp, IEnumerator urlEnum)
        {
            HttpContext context = ((HttpApplication)httpApp).Context;
            return HandleURL(context, urlEnum);
        }

        #region navigation
        /*****************************************************************************************/
        // Blog URL             = Blog.aspx
        /*****************************************************************************************/
        // Post URL             = Blog/Post/PostId/some.thing.imp.aspx
        /*****************************************************************************************/
        // Post URL by author   = Blog/Author/author name.aspx
        /*****************************************************************************************/
        // Post URL by category = Blog/Category/Category name.aspx
        /*****************************************************************************************/
        // Post URL by tag      = Blog/Tag/Tag name.aspx
        /*****************************************************************************************/
        // Post URL by date     = Blog/Archive/Year/Month.aspx
        /*****************************************************************************************/

        //                      FEEDs
        /*****************************************************************************************/
        // Blog RSS feed URL        = Blog/rss.aspx
        /*****************************************************************************************/
        // RSS feed URL by category = Blog/rss/Category/Category name.aspx
        /*****************************************************************************************/        
        // RSS feed URL for Comments = Blog/rss/Comments.aspx
        /*****************************************************************************************/

        #endregion

        private string HandleURL(HttpContext context, IEnumerator urlEnum)
        {
            string root = WebRoots.GetRelativeWebRoot;
            if (!urlEnum.MoveNext())
            { // Blog URL = Blog.aspx
                return root + "Default.aspx?PageId=" + BlogController.BlogPageId;
            }
            switch (((string)urlEnum.Current).ToLower())
            {
                case "rss.aspx":// blog RSS feed
                    RssFeedController.GenerateRssForLatestPosts(context);
                    break;
                case "rss":
                    if (urlEnum.MoveNext())
                    {
                        // blog RSS feed for Blog Categories
                        if (string.Compare((string)urlEnum.Current, "category", true) == 0)
                        {
                            if (urlEnum.MoveNext())
                                RssFeedController.GenerateRssForCategory(context, TrimExtension((string)urlEnum.Current));
                        }
                        // blog RSS feed for Blog Comments
                        if (string.Compare((string)urlEnum.Current, "comments.aspx", true) == 0)
                        {
                            RssFeedController.GenerateRssForLatestComments(context);
                        }
                    }
                    break;
                case "post":
                    if (urlEnum.MoveNext())// display the blog post
                    {
                        int postid = 0;
                        Int32.TryParse(TrimExtension((string)urlEnum.Current), out postid);
                        if (PostController.GetPost(postid) != null)
                        {
                            return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&PostId=" + postid;
                        }
                    }
                    break;
                case "page":
                    if (urlEnum.MoveNext())// display the blog post page
                    {
                        int page = 0;
                        Int32.TryParse(TrimExtension((string)urlEnum.Current), out page);
                        return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&PostsPage=" + page;
                    }
                    break;
                case "category":
                    if (urlEnum.MoveNext())
                    {
                        string categoryName = TrimExtension((string)urlEnum.Current);
                        if (urlEnum.MoveNext())// display the category page
                        {
                            if (string.Compare((string)urlEnum.Current, "page", true) == 0)                            
                            {
                                if (urlEnum.MoveNext())
                                {
                                    int page = 0;
                                    Int32.TryParse(TrimExtension((string)urlEnum.Current), out page);
                                    return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&Category=" + categoryName + "&CategoryPage=" + page;
                                }
                            }
                        }
                        return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&Category=" + categoryName;
                    }
                    break;
                case "tag":
                    if (urlEnum.MoveNext())
                    {
                        string tagName = TrimExtension((string)urlEnum.Current);
                        if (urlEnum.MoveNext())// display the tag page
                        {
                            if (string.Compare((string)urlEnum.Current, "page", true) == 0)
                            {
                                if (urlEnum.MoveNext())
                                {
                                    int page = 0;
                                    Int32.TryParse(TrimExtension((string)urlEnum.Current), out page);
                                    return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&Tag=" + tagName + "&TagPage=" + page;
                                }
                            }
                        }// display the blog posts by this tag name
                        return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&Tag=" + tagName;
                    }
                    break;
                //case "author":
                //    if (urlEnum.MoveNext())// display the blog posts by this author name
                //        return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&Author=" + TrimExtension((string)urlEnum.Current);
                //    break;
                case "archive":
                    if (urlEnum.MoveNext())// display the blog posts by archive
                    {// URL = Blog/Archive/Year/Month.aspx
                        string year = (string)urlEnum.Current;
                        if (urlEnum.MoveNext())
                        {
                            string month = TrimExtension((string)urlEnum.Current);
                            if (urlEnum.MoveNext())// display the Archive page
                            {
                                if (string.Compare((string)urlEnum.Current, "page", true) == 0)
                                {
                                    if (urlEnum.MoveNext())
                                    {
                                        int page = 0;
                                        Int32.TryParse(TrimExtension((string)urlEnum.Current), out page);
                                        return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&Year=" + year + "&Month=" + month + "&ArchivePage=" + page;
                                    }
                                }
                            }
                            return root + "Default.aspx?PageId=" + BlogController.BlogPageId + "&Year=" + year + "&Month=" + month;
                        }
                    }
                    break;
            }
            return null;
        }

        private string TrimExtension(string url)
        {
            if (url.EndsWith(".aspx"))
                return url.Remove(url.LastIndexOf('.'));
            else
                return url;
        }
    }
}
